cc.Class({
    extends: cc.Component,

    properties: {
         // 主角跳跃高度
        jumpHeight: 0,
        // 主角跳跃持续时间
        jumpDuration: 0,
        // 最大移动速度
        maxMoveSpeed: 0,
        // 加速度
        accel: 0,
        //跳跃的声音
        jumpAudio: {
            default: null,
            url: cc.AudioClip
        },
        de: false,
        positonX : 0,
        positionY : 0
    },

    setJumpAction: function () {
        // 跳跃上升
        var jumpUp = cc.moveBy(this.jumpDuration, cc.p(0, this.jumpHeight)).easing(cc.easeCubicActionOut());
        // 下落
        var jumpDown = cc.moveBy(this.jumpDuration, cc.p(0, -this.jumpHeight)).easing(cc.easeCubicActionIn());
        //回调函数 动作结束时调用
        var callback = cc.callFunc(this.playJumpSound, this);
        // 不断重复
        return cc.repeatForever(cc.sequence(jumpUp, jumpDown, callback));
    },
    playJumpSound: function () {
        // 调用声音引擎播放声音
        cc.audioEngine.playEffect(this.jumpAudio, false);
    },
    cocosLoad: function () {
        //放回起始位置
        this.node.setPosition(cc.p(this.positonX, this.positionY));
        // 初始化跳跃动作
        this.jumpAction = this.setJumpAction();
        this.node.runAction(this.jumpAction);
        
        // 初始化键盘输入监听
        this.setInputControl();
    },

    onLoad: function () {
        // 加速度方向开关
        this.accLeft = false;
        this.accRight = false;
         //主角当前水平方向速度
        this.xSpeed = 0;

        this.positonX = this.node.x;
        this.positionY = this.node.y;

        //开启重力感应
         cc.systemEvent.setAccelerometerEnabled(true);
    },

    setInputControl: function () {

        console.log("gamestar");
        var self = this;
        // 添加键盘事件监听
        // 有按键按下时，判断是否是我们指定的方向控制键，并设置向对应方向加速
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        // 松开按键时，停止向该方向的加速
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);

        cc.systemEvent.on(cc.SystemEvent.EventType.DEVICEMOTION,this.onDeviceMotion, this);
    },
    onKeyDown : function (event){
        switch(event.keyCode) {
            case cc.KEY.a:
                Global.player.accLeft = true;
                break;
            case cc.KEY.d:
                Global.player.accRight = true;
                break;
        }
    },
    onKeyUp : function (event){
        switch(event.keyCode) {
            case cc.KEY.a:
                Global.player.accLeft = false;
                break;
            case cc.KEY.d:
                Global.player.accRight = false;
                break;
        }
    },

    //根据手机的左右倾斜来设定往左跳还是往右跳
    onDeviceMotion: function(event) {
        if (Global.wxEnvironment) {
            wx.onAccelerometerChange(function (e) {
                if (e.x > 0) {
                    Global.player.accLeft = false;
                    Global.player.accRight = true;
                } else if (e.x < 0) {
                    Global.player.accLeft = true;
                    Global.player.accRight = false;
                }
            });
        }
        
        if (event.acc.x > 0) {
             Global.player.accLeft = false;
             Global.player.accRight = true;
        } else if(event.acc.x < 0){
             Global.player.accLeft = true;
             Global.player.accRight = false;
        }
    },
    update: function (dt) {
        // 根据当前加速度方向每帧更新速度
        if (this.accLeft == true) {
            this.xSpeed -= this.accel * dt;
        } else if (this.accRight) {
            this.xSpeed += this.accel * dt;
        }
        // 限制主角的速度不能超过最大值
        if ( Math.abs(this.xSpeed) > this.maxMoveSpeed ) {
            // if speed reach limit, use max speed with current direction
            this.xSpeed = this.maxMoveSpeed * this.xSpeed / Math.abs(this.xSpeed);
        }

        // 根据当前速度更新主角的位置
        this.node.x += this.xSpeed * dt;

        //不能移动超出屏幕边界
        if (this.node.x > Global.game.node.width/2){
            this.node.x =  Global.game.node.width/2;
            this.xSpeed = 0;
        }
        if (this.node.x < -Global.game.node.width/2) {
            this.node.x = - Global.game.node.width/2;
            this.xSpeed = 0;
        }
    },
});
